package com.mf.controller.pageConfig;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mf.entity.bo.pageConfig.BasicJsonConfigBo;
import com.mf.entity.request.pageConfig.JsonConfigAddReq;
import com.mf.entity.request.pageConfig.JsonConfigSearchReq;
import com.mf.service.pageConfig.IBasicJsonConfigService;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.VerifyUtils;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* 
*
* @author mf
* @since 2024-12-19
*/

@RestController
@RequestMapping("basic/jsonConfig/v1")
public class BasicJsonConfigController {
    @Resource
    private IBasicJsonConfigService jsonConfigService;

    @Operation(summary = "新增|编辑")
    @PostMapping("save")
    public Result<BasicJsonConfigBo> save(@Valid @RequestBody() JsonConfigAddReq jsonConfigAddReq) {
        BasicJsonConfigBo jsonConfigBo = new BasicJsonConfigBo();
        BeanUtils.copyProperties(jsonConfigAddReq, jsonConfigBo);

        if (VerifyUtils.isEmpty(jsonConfigAddReq.getId())) {
            jsonConfigService.save(jsonConfigBo);
            return Result.success(jsonConfigBo);
        }
        jsonConfigService.updateById(jsonConfigBo);
        return Result.success(jsonConfigBo);
    }

    @Operation(summary = "删除")
    @GetMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        return Result.success(jsonConfigService.removeById(id));
    }

    @Operation(summary = "分页查询")
    @PostMapping("findPageList")
    public Result<Page<BasicJsonConfigBo>> findPageList(@Valid @RequestBody() JsonConfigSearchReq jsonConfigSearchReq) {
        return Result.success(jsonConfigService.findPageList(jsonConfigSearchReq));
    }

    @Operation(summary = "不分页查询")
    @GetMapping("findList")
    public Result<List<BasicJsonConfigBo>> findList() {
        return Result.success(jsonConfigService.list());
    }

    @Operation(summary = "根据id查询配置")
    @GetMapping("findById")
    public Result<BasicJsonConfigBo> findById(@RequestParam("id") Integer id) {
        return Result.success(jsonConfigService.getById(id));
    }
}
